package user.service;

import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RequestBody;
import user.dto.UserDto;
import user.mapper.UserMapper;
import user.params.TypeUpdateDto;
import user.params.UserAddParam;
import user.params.UserLoginParam;

import java.util.Date;
import java.util.List;
@AllArgsConstructor
@DubboService(version = "1.0.0")
public class UserServiceImpl implements UserServiceI {
    private UserMapper userMapper;
    @Override
    public UserDto getUserById(int id){

        return userMapper.selectUserById(id);
    }
    public UserDto addUser(UserAddParam addUserParam){
        UserDto userDto = new UserDto();
        userDto.setUsername(addUserParam.getUsername());
        userDto.setPassword(addUserParam.getPassword());
        userDto.setEmail(addUserParam.getEmail());
        userDto.setCreatedAt(new Date());
        userDto.setHeight(addUserParam.getHeight());
        userDto.setWeight(addUserParam.getWeight());
        userDto.setAge(addUserParam.getAge());
        userDto.setSex(addUserParam.getSex());
        userDto.calculateNutrientNeeds();
        userMapper.insertUser(userDto);
        return userDto;
    }
   public void delUser(int userId){
        userMapper.delUser(userId);
    }
    public UserDto login(UserLoginParam userLoginParam){
        UserDto userDto = userMapper.SelectUserByNameAndPassword(userLoginParam.getUsername(),userLoginParam.getPassword());
        return userDto;
    }
    public List<UserDto> getAllUsers(){

        return userMapper.selectAllUsers();
    }

    @Override
    public UserDto getUserByName(String username) {
        return userMapper.SelectUserByName(username);
    }

    @Override
    public String updatePassword(String username, String password) {
         userMapper.updateUserPassword(username,password);
         return "success";
    }

    @Override
    public String updateUserAICount(UserDto user) {
        userMapper.updateAICount(user.getUserId(),user.getAICount());
        return "success";
    }

    @Override
    public String getUserType(int userId) {
        return userMapper.getUserType(userId);
    }

    @Override
    public String updateUserType(TypeUpdateDto typeUpdateDto) {
        userMapper.updateUserOfType(typeUpdateDto);
        return "success";
    }

}
